<?php
namespace App\Builder;
use Illuminate\Database\Query\Builder;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Models\FactoryDevice;
use Illuminate\Support\Facades\DB;
class FactoryDeviceBuilder{
    public function listBuilder($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): Builder
    {
        $authUser = auth()->user();
        $role = $authUser->role;
        $query = DB::table((new FactoryDevice())->getTable());
        if($role->code=='FACTORY'){
            $query->where('user_id',$authUser->id);
        }
        if($args['sort'] == 'DESC'){
            $query->latest();
        }elseif($args['sort'] == 'ASC'){
            $query->oldest();
        }
        if($args['archive_status']){
            $query->where('archive_status',$args['archive_status']);
        }
        return $query;
    }
}